﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace XRpgLibrary.TileEngine
{
    public class TileAnimation : List<TileFrame>
    {
        TimeSpan frameTimer = TimeSpan.Zero;
        int currentFrame = 0;

        public int FirstTileIndex
        {
            get
            {
                if (this.Count > 0)
                    return this[0].TileIndex;

                return -1;
            }
        }

        public int CurrentTileIndex
        {
            get
            {
                return this[currentFrame].TileIndex;
            }
        }

        public void Update(GameTime gameTime)
        {
            if (this[currentFrame].FrameDuration == TimeSpan.Zero)
                return;

            frameTimer += gameTime.ElapsedGameTime;
            if (frameTimer >= this[currentFrame].FrameDuration)
            {
                frameTimer = TimeSpan.Zero;
                currentFrame = (currentFrame + 1) % this.Count;
            }
        }

        public TileAnimation Clone()
        {
            TileAnimation ta = new TileAnimation();

            foreach (TileFrame frame in this)
                ta.Add(frame.Clone());

            return ta;
        }

        public static TileAnimation GenerateTileAnimation(string script)
        {
            TileAnimation animation = new TileAnimation();
            string original = script;
            //start with frame 4 for .1 seconds, go to 5 for .1 seconds, go to 6 for .1 seconds
            //4 .1 5 .1 6 .1

            //shorthand same as above
            //4 ... 6 .1

            //the last frame is 0 seconds, so the animation will stop on frame 6 forever (unless altered by script)
            //4 .1 5 .1 6 0

            //count by 2
            //4 ..2 12 .25

            try
            {
                
                while (script != string.Empty)
                {
                    string sTileIndex = script.Substring(0, script.IndexOf(' '));
                    script = script.Substring(script.IndexOf(' ') + 1);

                    int len = script.IndexOf(' ') > -1 ? script.IndexOf(' ') : script.Length;
                    string sDuration = script.Substring(0, len);
                    if (script.Length > len)
                        script = script.Substring(len + 1);
                    else
                        script = string.Empty;

                    int tileIndex = int.Parse(sTileIndex);

                    if (sDuration.StartsWith(".."))
                    {
                        string sFinalTileIndex = script.Substring(0, script.IndexOf(' '));
                        script = script.Substring(script.IndexOf(' ') + 1);

                        int rangelen = script.IndexOf(' ') > -1 ? script.IndexOf(' ') : script.Length;
                        string sFinalDuration = script.Substring(0, rangelen);
                        if (script.Length > rangelen)
                            script = script.Substring(rangelen + 1);
                        else
                            script = string.Empty;

                        int finalTileIndex = int.Parse(sFinalTileIndex);
                        double finalDuration = double.Parse(sFinalDuration);

                        sDuration = sDuration.Substring(2);
                        int step = 1;
                        if (sDuration != ".")
                            step = int.Parse(sDuration);

                        for (int i = tileIndex; i <= finalTileIndex; i += step)
                            animation.Add(new TileFrame(i, finalDuration));
                    }
                    else
                    {
                        double duration = double.Parse(sDuration);

                        animation.Add(new TileFrame(tileIndex, duration));
                    }
                }
            }
            catch
            {
                Logging.Log.Error("Animation Script was malformed: '{0}'", original);
            }

            return animation;
        }
    }
}
